/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.account;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysPermission;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-09
 */
@Service("syspermissionService")
public class SysPermissionServiceImpl implements SysPermissionService {
    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of SysPermission
     ****************************************/
    public void insert(SysPermission syspermission) throws Exception {
        syspermission.setPermitId(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(syspermission);
    }

    /****************************************
     * Update a instace of SysPermission
     ****************************************/
    public void update(SysPermission syspermission) throws Exception {
        iDataSet4iBatis.update(syspermission);
    }

    /****************************************
     * Delete a instace of SysPermission
     ****************************************/
    public void delete(SysPermission syspermission) throws Exception {
        iDataSet4iBatis.delete(syspermission);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(SysPermission syspermission) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(SysPermission syspermission) throws Exception {
        String statementId = syspermission.getStatmentId();
        String mainId = syspermission.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> syspermissionList = new ArrayList<BaseModel>();
        for (String id : ids) {
            SysPermission model = new SysPermission();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            syspermissionList.add(model);
        }
        iDataSet4iBatis.batchDelete(syspermissionList);
    }

    /****************************************
     * Query instaces of SysPermission
     ****************************************/
    public List<BaseModel> queryAsList(SysPermission syspermission) throws Exception {
        String queryListStatement = "";
        if (syspermission.isPaging()) {
            queryListStatement = syspermission.getStatmentId();
            syspermission.setStatmentId("SysPermission.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(syspermission);
            syspermission.setTotalCount(baseModel.getTotalCount());
            syspermission.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(syspermission);
    }

    /****************************************
     * Query a instaces of SysPermission
     ****************************************/
    public BaseModel queryAsObject(SysPermission syspermission) throws Exception {
        return iDataSet4iBatis.queryAsObject(syspermission);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
